package service.person;

import dao.community.CommunityDao;
import dao.community.CommunityDaoImpl;
import dao.person.PersonDao;
import dao.person.PersonDaoImpl;
import domain.Community;
import domain.Info;
import domain.Page;
import domain.Room;

import java.util.ArrayList;
import java.util.List;

public class PersonServiceImpl implements PersonService {
    PersonDao personDao = new PersonDaoImpl();
    /**
     * 新增常住信息
     * @param community_id,room_id,name,sex,phone,idcard,identity_img1,identity_img2,photo_img,carid
     * @return int
     * @author 夏添
     * @date 2020/7/23 9:25
     */
    @Override
    public int CreatPerson(int community_id,int room_id,String name,String sex,String phone,String idcard,String identity_img1,String identity_img2,String photo_img,String carid){
        int i =0;
        try {
            i=personDao.CreatPerson(community_id,room_id,name,sex,phone,idcard,identity_img1,identity_img2,photo_img,carid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 通过住房id检索该住房下的常驻人口
     * @param room_id
     * @return info
     * @author 夏天
     * @date 2020/7/23 9:39
     */
    public ArrayList<Info> UserPerson(int room_id){
        ArrayList<Info> list = new ArrayList<>();
        try {
            list = personDao.UserPerson(room_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 删除住房下的常驻人口
     * @param info_id
     * @return int
     * @author 夏天
     * @date 2020/7/23 11:09
     */
    public  int DelectPerson(int info_id){
        int i =0;
        try {
            i = personDao.DelectPerson(info_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 更新常驻人口信息
     * @param info_id,phone,idcard,identity_img1,identity_img2,photo_img,carid
     * @return int
     * @author 夏天
     * @date 2020/7/23 11:23
     */
    public  int UpdatePerson(int info_id,String phone,String idcard,String identity_img1,String identity_img2,String photo_img,String carid){
        int i =0;
        try {
            i = personDao.UpdatePerson(info_id,phone,idcard,identity_img1,identity_img2,photo_img,carid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  i;
    }
    /**
     * 通过id获取完整信息
     * @param info_id
     * @return info
     * @author 夏天
     * @date 2020/7/23 13:40
     */
    public Info getInfoById(int info_id) {
        Info info = new Info();
        try {
            info = personDao.getInfoById(info_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return info;
    }

    /**
     * 获取允许查看的住户数量
     * @param view_power
     * @return int
     * @author 夏天
     * @date 2020/7/23 14:06
     */
    public int getCount(String view_power) {
        int count = 0;
        if(view_power.equals("all")){
            view_power = "";
            CommunityDao communityDao = new CommunityDaoImpl();
            try {
                List<Community> list = communityDao.getCommunityList();
                for (int i =0; i<list.size();i++){
                    view_power += list.get(i).getCommunity_id()+",";
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (view_power ==null|| view_power.equals("") || view_power.equals("null"))
            return 0;
        else {
            try {
                count = personDao.getCount(view_power);
            }catch (Exception e){
                e.printStackTrace();
            }
            return count;
        }
    }

    /**
     * 获取符合条件的住户数量
     * @param view_power,community_id,query
     * @return int
     * @author 夏天
     * @date 2020/7/23 14:06
     */
    @Override
    public int getCount(String view_power,int community_id,String query){
        if(view_power.equals("all")){
            view_power = "";
            CommunityDao communityDao = new CommunityDaoImpl();
            try {
                List<Community> list = communityDao.getCommunityList();
                for (int i =0; i<list.size();i++){
                    view_power += list.get(i).getCommunity_id()+",";
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        int i = 0;
        if ( view_power.equals("null") || view_power.equals( "Null"))
            view_power = null;
        try{
            i = personDao.getCount(view_power,community_id,query);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  i ;
    }
    /**
     * 常驻人员后台检索
     * @param page,view_power
     * @return ArrayList
     * @author 夏添
     * @date 2020/7/20 20:16
     */
    @Override
    public ArrayList<Info> findByPage(Page page, String view_power){
        if(view_power.equals("all")){
            view_power = "";
            CommunityDao communityDao = new CommunityDaoImpl();
            try {
                List<Community> list = communityDao.getCommunityList();
                for (int i =0; i<list.size();i++){
                    view_power += list.get(i).getCommunity_id()+",";
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (view_power.equals("")||view_power.equals("null")||view_power.equals("Null"))
            view_power = null ;
        ArrayList<Info> list =new ArrayList<>();
        try {
            list = personDao.findByPage(page,view_power);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 常驻人员信息搜索
     * @param page,community_id,query,view_power
     * @return ArrayList
     * @author 夏添
     * @date 2020/7/23 20:32
     */
    @Override
    public ArrayList<Info> QueryPerson(Page page,int community_id,String query,String view_power){
        if(view_power.equals("all")){
            view_power = "";
            CommunityDao communityDao = new CommunityDaoImpl();
            try {
                List<Community> list = communityDao.getCommunityList();
                for (int i =0; i<list.size();i++){
                    view_power += list.get(i).getCommunity_id()+",";
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (view_power .equals( "null") || view_power.equals( "Null"))
            view_power = null;
        ArrayList<Info> list = new ArrayList<>();
        try {
            list = personDao.QueryPerson(page,community_id,query,view_power);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
    /**
     * 审核通过
     * @param info_id
     * @return int
     * @author 夏天
     * @date 2020/7/23 15:57
     */
    public int StateYes(int info_id){
        int i = 0;
        try {
            i = personDao.StateYes(info_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 审核不通过
     * @param info_id
     * @retrun int
     * @author 夏天
     * @date 2020/7/23 15:58
     */
    public int StateNo(int info_id){
        int i = 0;
        try {
            i = personDao.StateNo(info_id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

}
